package com.yoomiito.app.ui.college.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smartrefresh.header.MaterialHeader
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.colleage.Subject
import com.yoomiito.app.ui.college.detail.SubjectDetailActivity
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.refresh_rcy_layout.*
import kotlinx.android.synthetic.main.title_bar.*
import kotlinx.android.synthetic.main.title_framelayout.*

class CollegeSubjectActivity : BaseActivity<CollegeSubjectPresenter>() {

    companion object{
        fun start(context: Context){
            val intent = Intent(context, CollegeSubjectActivity::class.java)
            context.startActivity(intent)
        }
    }
    private var mCurrentPage = 1
    private val mCollegeAdapter = CollegeSubjectAdapter(null)

    override fun getLayoutId(): Int = R.layout.title_framelayout

    override fun newP(): CollegeSubjectPresenter = CollegeSubjectPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        tv_center.text = "商学院"
        iv_back_left.setOnClickListener { finish() }

        mStatusManager = StatusManager.Builder(this)
                .addType(ContentType(R.layout.refresh_rcy_layout))
                .addType(EmptyType().setEmptyContent("暂无内容"))
                .build()
        frameLayout.addView(mStatusManager.view())

        initRcy()
        initRefreshLayout()
    }

    private fun initRcy() {
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(12f),DensityUtil.dp2px(10f),true))
        rcy.itemAnimator = androidx.recyclerview.widget.DefaultItemAnimator()
        mCollegeAdapter.setOnItemClickListener { _, _, position ->
            val subject = mCollegeAdapter.data[position]
            SubjectDetailActivity.start(this,subject.id,subject.isHas_buy,subject.product_id)
        }
        rcy.adapter = mCollegeAdapter
    }

    private fun initRefreshLayout() {
        refreshLayout.setRefreshHeader(MaterialHeader(this))
                .setRefreshFooter(ClassicsFooter(this))
                .setOnRefreshListener {
                    mCurrentPage = 1
                    p.queryList(mCurrentPage)
                }
                .setOnLoadMoreListener {
                    mCurrentPage++
                    p.queryList(mCurrentPage)
                }
                .setBackgroundResource(R.color.color_F4F4F4)
        refreshLayout.autoRefresh()
    }

    fun setData(subjectList:BaseList<Subject>){
        mCurrentPage = subjectList.current_page
        if (mCurrentPage==1){
            if (subjectList?.data == null ||subjectList.data.size==0){
                mStatusManager.showEmpty()
            }else{
                mCollegeAdapter.setNewData(subjectList.data)
            }
            refreshLayout.finishRefresh(true)
        }else{
            mCollegeAdapter.addData(subjectList.data)
            refreshLayout.finishLoadMore(false)
        }
        if (subjectList.current_page==subjectList.last_page)refreshLayout.finishLoadMoreWithNoMoreData()
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (eventMessage?.eventType.equals(AllCode.UPDATE_DATA)){
            refreshLayout.autoRefresh()
        }
    }

}